local skel = fk.CreateSkill {
  name = "lb__lihuan",
}

Fk:loadTranslationTable {
  ["lb__lihuan"] = "离焕",
  [":lb__lihuan"] = "<b>锁定技</b>，你未展示过的手牌对你隐藏，且这些牌的手牌上限等于你已损失的体力值。每回合限两次，出牌阶段或当你需要抵消牌时，你可展示任意张隐藏手牌，视为使用或打出其中一张基本牌或普通锦囊牌并重铸展示牌。若未因此使用或打出牌，你可视为使用一张【火攻】，结算后受到此牌伤害的角色回复1点体力。",


  ["lb__lihuan_use"] = "离焕",
  ["#lb__lihuan"] = "离焕：你可以展示任意张隐藏手牌，视为使用或打出其中一张基本牌或普通锦囊牌并重铸展示牌",
  ["#lb__lihuan_discard"] = "离焕：你须弃置%arg张隐藏牌",
  ["@lb__lihuan"] = "离焕",

  ["$lb__lihuan1"] = "流云易散，长梦已醒。",
  ["$lb__lihuan2"] = "生命纵如微尘。",
  ["$lb__lihuan3"] = "只要彼此挂念，总有重逢之时。 ",
}

skel:addEffect("viewas", {
  anim_type = "defensive",
  prompt = "#lb__lihuan",
  pattern = "jink,nullification",
  card_filter = Util.FalseFunc,
  interaction = function(self, player)
    local all_names = { "jink", "nullification" }
    return UI.CardNameBox {
      choices = player:getViewAsCardNames("lb__lihuan", all_names),
      all_choices = all_names,
    }
  end,
  view_as = function(self, player, cards)
    if Fk.all_card_types[self.interaction.data] == nil then return end
    local c = Fk:cloneCard(self.interaction.data)
    c.skillName = skel.name
    return c
  end,
  before_use = function(self, player, use)
    local room = player.room
    local cards_yc = table.filter(player:getCardIds("h"), function(id)
      return Fk:getCardById(id):getMark("lb__lihuan-inhand") == 0
    end)
    if #cards_yc > 0 then
      local pattern = ".|.|.|.|.|.|" .. table.concat(cards_yc, ",")
      local cards = room:askToCards(player, {
        min_num = 1,
        max_num = 999,
        include_equip = false,
        skill_name = "lb__lihuan",
        cancelable = true,
        pattern = pattern,
        prompt = "#lb__lihuan",
      })
      if #cards > 0 then
        local card_names = {}
        player:showCards(cards)
        for _, id in ipairs(cards) do
          local card = Fk:getCardById(id)
          if table.contains(Fk:getAllCardNames("bt"), card.name) then
            table.insertIfNeed(card_names, card.name)
          end
        end
        if table.contains(card_names, use.card.name) then
          cards = table.filter(cards, function(id)
            return (room:getCardArea(id) == Card.PlayerHand) and (room:getCardOwner(id) == player)
          end)
          if #cards > 0 then
            room:recastCard(cards, player, "lb__lihuan")
          end
          return
        else
          room:askToUseVirtualCard(player, {
            name = "fire_attack",
            skill_name = "lb__lihuan_use",
          })
        end
      end
    end
    return skel.name
  end,
  enabled_at_play = function(self, player)
    return table.find(player:getCardIds("h"), function(id)
      return Fk:getCardById(id):getMark("lb__lihuan-inhand") == 0
    end) ~= nil and player:usedSkillTimes("lb__lihuan") < 2
  end,
  enabled_at_response = function(self, player, response)
    return table.find(player:getCardIds("h"), function(id)
      return Fk:getCardById(id):getMark("lb__lihuan-inhand") == 0
    end) ~= nil and player:usedSkillTimes("lb__lihuan") < 2
  end,
})

skel:addEffect(fk.Damaged, {
  can_refresh = function(self, event, target, player, data)
    return target == player and data.card and data.card.skillName == "lb__lihuan_use"
  end,
  on_refresh = function(self, event, target, player, data)
    data.card.extra_data = data.card.extra_data or {}
    data.card.extra_data.lb__lihuan_damage = data.card.extra_data.lb__lihuan_damage or {}
    table.insertIfNeed(data.card.extra_data.lb__lihuan_damage, target.id)
  end,
})

skel:addEffect(fk.CardUseFinished, {
  can_refresh = function(self, event, target, player, data)
    return target == player and data.card.extra_data and data.card.extra_data.lb__lihuan_damage
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room:getAlivePlayers()) do
      if table.contains(data.card.extra_data.lb__lihuan_damage, p.id) then
        room:recover {
          who = p,
          num = 1,
          skillName = "lb__lihuan",
        }
        room:delay(150)
      end
    end
  end,
})

skel:addEffect("visibility", {
  card_visible = function(self, player, card)
    if player:hasSkill("lb__lihuan") and table.contains(player:getCardIds("h"), card.id)
        and card:getMark("lb__lihuan-inhand") == 0 then
      return false
    end
  end
})

skel:addEffect("filter", {
  mute = true,
  card_filter = function(self, card, player)
    return player:hasSkill("lb__lihuan") and table.contains(player:getCardIds("h"), card.id)
        and card:getMark("lb__lihuan-inhand") == 0
  end,
  view_as = function(self, player, card)
    local c = Fk:cloneCard(card.name, card.suit, card.number)
    c.special_skills = nil
    return c
  end,
})

skel:addEffect(fk.CardShown, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill("lb__lihuan")
  end,
  on_refresh = function(self, event, target, player, data)
    for _, id in ipairs(data.cardIds) do
      player.room:setCardMark(Fk:getCardById(id), "lb__lihuan-inhand", 1)
    end
  end,
})

skel:addEffect(fk.EventPhaseStart, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill("lb__lihuan") and player.phase == Player.Discard
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local yc = player.maxHp - player.hp
    local cards_yc = table.filter(
      player:getCardIds(Player.Hand), function(id)
        local card = Fk:getCardById(id)
        return card:getMark("lb__lihuan-inhand") == 0
      end
    )
    local n = #cards_yc - yc
    if n > 0 then
      local dis
      if yc == 0 then
        dis = cards_yc
      else
        local pattern = ".|.|.|.|.|.|" .. table.concat(cards_yc, ",")
        dis = room:askToCards(player, {
          min_num = n,
          max_num = n,
          include_equip = false,
          skill_name = "phase_discard",
          cancelable = false,
          pattern = pattern,
          prompt = "#lb__lihuan_discard:::" .. n,
        })
      end
      room:throwCard(dis, "phase_discard", player, player)
    end
  end,
})

local lihuan_jin = function(self, player, card)
  if player:hasSkill("lb__lihuan") and table.contains(player:getCardIds("h"), card.id) and
      card:getMark("lb__lihuan-inhand") == 0 then
    return true
  end
end

skel:addEffect("maxcards", {
  exclude_from = lihuan_jin,
})

skel:addEffect("prohibit", {
  prohibit_use = lihuan_jin,
  prohibit_response = lihuan_jin,
  prohibit_discard = lihuan_jin,
})

skel:addEffect(fk.TurnStart, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark("@lb__lihuan") ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@lb__lihuan", 0)
    player.room:validateSkill(player, "lb__lihuan")
  end,
})

skel:addEffect(fk.EventPhaseStart, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill("lb__lihuan", true) and player.phase == Player.Play and
    not player:hasSkill("lb__lihuan_active", true)
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:handleAddLoseSkills(player, "lb__lihuan_active", skel.name, false, true) --好像没必要
  end,
})

skel:addAcquireEffect(function(self, player, is_start)
  local room = player.room
  room:handleAddLoseSkills(player, "lb__lihuan_active", skel.name, false, true)
end)

skel:addLoseEffect(function (self, player, is_death)
  local room = player.room
  room:setPlayerMark(player, "@lb__lihuan", 0)
  room:validateSkill(player, skel.name)
end)

return skel
